﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace MS.Utility.Files
{
    /// <summary>
    /// Stream接收
    /// </summary>
    public class UploadStreamImage
    {
        public MemoryStream stream = null;



        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="bytes">文件</param>
        /// <param name="_path">文件上传的文件路径（filePath）</param>
        public UploadStreamImage(byte[] bytes, string _path)
        {
            if (bytes.Count() > 0)
            {
                this.stream = new MemoryStream(bytes);
            }
            this.path = Path.Combine(_path, DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM-dd"));
        }

        public string path { get; set; }

        /// <summary>
        /// 确定上传类型
        /// </summary>
        protected bool IsUplodType
        {
            get
            {
                string extension = this.GetExtension();
                return ".jpg .gif .png .icon .bmp .tiff .wmf .emf .exif".IndexOf(extension) >= 0 ? true : false;
            }
        }

        private Image File
        {
            get
            {
                return this.stream == null ? null : Image.FromStream(this.stream);
            }
        }

        private string _fileName = null;
        /// <summary>
        /// 最终保存路径
        /// </summary>
        protected string FileName
        {
            set { _fileName = value; }
            get { return _fileName; }
        }


        /// <summary>
        /// 保存图片,成功返回文件路径,失败null
        /// 非图片格式返回 1 
        /// </summary>
        /// <returns>成功返回文件路径 失败null</returns>
        public string Save()
        {
            if (!this.IsUplodType)
            {
                this.stream.Dispose();
                return "1";
            }
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            this.FileName = GetFileName();
            string SavePath = Path.Combine(path, this.FileName);
            if (this.FileName != null)
            {
                this.File.Save(SavePath);
                //this.stream.Dispose();
                return DateTime.Now.ToString("yyyy")+"/"+DateTime.Now.ToString("MM-dd")+"/"+this.FileName;
            }
            return null;
        }


        /// <summary>
        /// 获取最终保存文件名
        /// </summary>
        /// <returns></returns>
        protected string GetFileName()
        {
            string extension = this.GetExtension();
            if (null == extension) return null;
            else
            {
                string name = DateTime.Now.ToString("yyyyMMddHHmmssffff"); ;
                string[] imgName = { name, extension };
                return string.Join("", imgName);
            }
        }

        protected string GetExtension()
        {
            if (this.File != null)
            {
                string fileExtension = this.File.RawFormat.ToString().Substring(14),
                       jpgExtension = System.Drawing.Imaging.ImageFormat.Jpeg.Guid.ToString(),
                       gifExtension = System.Drawing.Imaging.ImageFormat.Gif.Guid.ToString(),
                       pngExtension = System.Drawing.Imaging.ImageFormat.Png.Guid.ToString(),
                       iconExtension = System.Drawing.Imaging.ImageFormat.Icon.Guid.ToString(),
                       bmpExtension = System.Drawing.Imaging.ImageFormat.Bmp.Guid.ToString(),
                       tiffExtension = System.Drawing.Imaging.ImageFormat.Tiff.Guid.ToString(),
                       wmfExtension = System.Drawing.Imaging.ImageFormat.Wmf.Guid.ToString(),
                       emfExtension = System.Drawing.Imaging.ImageFormat.Emf.Guid.ToString(),
                       exifExtension = System.Drawing.Imaging.ImageFormat.Exif.Guid.ToString();
                fileExtension = fileExtension.Substring(0, fileExtension.Length - 1);
                if (fileExtension == jpgExtension)
                {
                    return ".jpg";
                }
                else if (fileExtension == gifExtension)
                {
                    return ".gif";
                }
                else if (fileExtension == pngExtension)
                {
                    return ".png";
                }
                else if (fileExtension == iconExtension)
                {
                    return ".icon";
                }
                else if (fileExtension == bmpExtension)
                {
                    return ".bmp";
                }
                else if (fileExtension == tiffExtension)
                {
                    return ".tiff";
                }
                else if (fileExtension == wmfExtension)
                {
                    return ".wmf";
                }
                else if (fileExtension == emfExtension)
                {
                    return ".emf";
                }
                else if (fileExtension == exifExtension)
                {
                    return ".exif";
                }
            }
            return null;
        }
    }
}
